package com.honey.mybatis.generator.dao.method;

import com.honey.compilation.java.AbstractJavaCompilation;
import com.honey.compilation.java.IfBlock;
import com.honey.compilation.java.JavaKeyWord;
import com.honey.compilation.java.Method;
import com.honey.compilation.java.Parameter;
import com.honey.core.dbmapping.introspect.IntrospectSchema;
import com.honey.core.dbmapping.introspect.IntrospectSchemaColumn;
import com.honey.core.dbmapping.structure.Column;
import com.honey.core.dbmapping.structure.TableColumn;
import com.honey.core.types.FullyQualifiedJavaType;
import com.honey.core.types.JDKFullyQualifiedJavaType;
import com.honey.core.utils.StringUtility;
import com.honey.mybatis.generator.MybatisPlugin;
import com.honey.mybatis.generator.dao.DaoConstant;

/**
 * 按照primaryKey查询
 * @author Administrator
 *
 */
public class DeleteByDynamicMethod extends AbstractCreateInterfaceMethod{

	private static final String METHOD_NAME="deleteByDynamic";
		
	private static final FullyQualifiedJavaType RETURN_TYPE = JDKFullyQualifiedJavaType.getIntInstance();
	
	private boolean isUseSession = false ;
	
	boolean enable = false ;
	
	public DeleteByDynamicMethod(
			AbstractJavaCompilation clazz, MybatisPlugin belongPlugin,
			IntrospectSchema introspectSchema,
			IntrospectSchemaColumn introspectSchemaColumn
			) {
		super(clazz, belongPlugin, introspectSchema, introspectSchemaColumn);
		isEnable();
	}
	
	public DeleteByDynamicMethod(
			AbstractJavaCompilation clazz, MybatisPlugin belongPlugin,
			IntrospectSchema introspectSchema,
			IntrospectSchemaColumn introspectSchemaColumn,
			boolean  isUseSession
			) {
		super(clazz, belongPlugin, introspectSchema, introspectSchemaColumn);
		this.isUseSession = isUseSession;
		isEnable();
	}
	
	private void isEnable(){
		if( mybatisPlugin.isEnableVirtualDeleteKey()){
			for(Column column : introspectSchemaColumn.allColumns() ){
				String value = ((TableColumn)column).getVirtualDeleteValue();
				if( StringUtility.stringHasValue(value)){
					enable = true;
					break;
				}
			}
		}
	}
	/* (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#addBody(com.honey.compilation.java.Method)
	 */
	@Override
	protected void addBody(Method method) {	
		if( enable ){
			Parameter par = new Parameter(JDKFullyQualifiedJavaType.getBoolInstance(),"isPhysicDel" );
			par.addJavaDocLine("是否物理删除 true:是 false:虚拟删除");
			method.addParameter(par);
			method.addJavaDocLine("启用的虚拟删除功能,默认使用虚拟删除. ");
		}
		
		IfBlock ifBlock = null;
		if( isUseSession()){
			ifBlock = new IfBlock(new StringBuilder().append( DaoConstant.SQL_SESSION_NAME ).append(" == null").toString());
			ifBlock.addBodyLine("return -1");
			method.addBlocks(ifBlock);
		}
		ifBlock = new IfBlock(new StringBuilder()
			.append(DaoConstant.CONDITION_VARIABLE_NAME)
			.append(" == null") 
			.append( " || "+ DaoConstant.CONDITION_VARIABLE_NAME +".getOredCriteria().size() == 0" )
			.toString()
		);
		ifBlock.addBodyLine(DaoConstant.CONDITION_VARIABLE_NAME+" = null");
		method.addBlocks(ifBlock);	
		
		method.addBodyLine(
				new StringBuilder()
				.append(JavaKeyWord.RETURN)
				//.append(JavaKeyWord.SUPER.getValue()).append('.')
				.append(mybatisPlugin.getDependentModel().call(null))
				.append("delete")
				.append( JavaKeyWord.OPERATION_BRACKET_LEFT )
				.append( isUseSession()?DaoConstant.SQL_SESSION_NAME+" , ":""  )
				//.append( DaoConstant.DELETE_BY_DYNAMIC_FIELD_NAME )
				//.append( super.getMapperId().getDeleteByDynamicMapperId() )
				.append( (enable ? " isPhysicDel ? ":"") + super.getMapperId().getDeleteByDynamicMapperId()+ (enable ? (" : "+ super.getMapperId().getVirtualDeleteByDynamicMapperId()):"" ))
				
				.append(" , ")
				.append(DaoConstant.CONDITION_VARIABLE_NAME).append(" )")
				.toString()
		);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getMethodName()
	 */
	@Override
	protected String getMethodName() {		
		String answer = null;
		if( mybatisPlugin.isEnableDelete() ){
			if( !isUseSession ){
				answer = METHOD_NAME;
			}else if( isUseSession  && mybatisPlugin.isEnableUseSqlSession() ){
				answer = METHOD_NAME;
			}
		}
		return answer;
	}
	
	/**
	 * @return the isUseSession
	 */
	@Override
	public final boolean isUseSession() {
		return isUseSession;
	}

	/* (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getJavaDoc()
	 */
	@Override
	protected String[] getJavaDoc() {
		String[] answer = new String[]{
			"动态删除记录集。"
		};
		return answer;
	}

	/* (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getReturnType()
	 */
	@Override
	protected FullyQualifiedJavaType getReturnType() {
		
		return RETURN_TYPE;
	}
}
